<template>
	<view>
		<tab-nav colType="lightYellBg" :orderStaIndex="orderStaIndex" :orderStaArr="tabList" @switchStatus="switchStatus"></tab-nav>
		<template v-if="isHasDataFlag">
			<bbs-list fromPage="myBbsPage" :typeId="typeId" :bbsList="bbsList" @cancelFavor="cancelFavor" @goBbsDet="goBbsDet"></bbs-list>
		</template>
		<template v-else-if="isHasDataFlag == false">
			<no-data></no-data>
		</template>
	</view>
</template>

<script>
	import TabNav from '@/components/tab-nav/tab-nav.vue'
	import BbsList from './components/BbsList'
	import { getMyMotify,collectMotify } from '@/api/zhuxue'
	export default {
		components:{
			TabNav,
			BbsList
		},
		data(){
			return{
				tabList:[
					{id:2,name:'我的发帖'},
					{id:1,name:'我的收藏'},
					{id:3,name:'我的回复'}
				],
				orderStaIndex:0,
				current: 0, 
				//courseListCls:'courseListTab',
				typeId:2,
				nowPage:1,
				loading:false,
				isHasDataFlag:null,
				bbsList:[],
				collectFlag:true
			}
		},
		onLoad(){
			this.loadMyBbs();
		},
		onPullDownRefresh : function(){
			this.initParams();
			uni.stopPullDownRefresh();
		},
		onReachBottom : function(){
			if( !this.loading ){
				this.loadMyBbs();
			}
		},
		methods:{
			cancelFavor(id){
				if(this.collectFlag){
					this.collectFlag = false;
					var field = {  account:uni.getStorageSync('token'),id };
					let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
					collectMotify({content:contentStr}).then(res=>{
						//console.log(res)
						this.util.showToastSuc('取消成功');
						setTimeout(()=>{
							this.initParams();
						},1800)
					}).catch(err=>{
						this.collectFlag = true;
						console.log(err)
					});
				}
			},
			goBbsDet(id){
				uni.navigateTo({
					url:'/pages/subIndex/bbsCenter/bbsDet?detId=' + id
				})
			},
			loadMyBbs(){ 
				let { nowPage,bbsList } = this;
				this.loading = true;
				var field = { account:uni.getStorageSync('token'),tag:this.typeId,pageNo:this.nowPage,pageSize:this.limit };
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				//console.log(field)
				getMyMotify({content:contentStr}).then(res=>{
					//console.log(res)
					if(this.nowPage == 1){
						if(res.data.datas.dList.length == 0){
							this.loading = false;
							this.isHasDataFlag=false;
						}
					}
					if(res.data.datas.dList.length > 0){
						nowPage += 1;
						bbsList.push( ...res.data.datas.dList );
						this.bbsList = bbsList;
						this.nowPage = nowPage;
						this.loading = false;
						this.isHasDataFlag=true;
					}else{
						if(this.nowPage > 1){
							this.loading = false;
						}
					}
				}).catch(err=>{
					console.log(err)
				})
			},
			initParams(){
				this.nowPage = 1;
				this.loading = false;
				this.bbsList = [];
				this.collectFlag = true;
				this.loadMyBbs();
			},
			switchStatus(index,id){
				this.orderStaIndex = index;
				this.typeId = id;
				this.initParams();
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: $pss-bg-primary;
	}
</style>